/*
 * Copyright(c) 2008 GENIETEK
 *
 * Linux System V message queue
 *
 *
 */
#include "msgtool.h"

int msgqueue_id;

int init_message_queue(void)
{
	key_t key;

	/* Create unique key via call to ftok() */
	key = ftok(".", 'm');

	/* Open the queue */
	if((msgqueue_id = msgget(key, IPC_CREAT|IPC_EXCL|0660)) == -1) 
	{
		printf("A : Message queue already exists!\n");
		return 0;
	}

	return 1;
}

void send_message(Message *qbuf, int flag)
{
	/* Send a message to the queue */
	printf("A : Sending a message ....Type: %ld Commond %d Text: %s\n", qbuf->mtype, qbuf->mcommond, qbuf->mtext);

	if((msgsnd(msgqueue_id, qbuf, sizeof(Message) - 4, flag)) ==-1)
		printf("A : msgsnd failed!\n");
	else
		printf("A : msgsnd success!\n");
}

int read_message(Message *qbuf, int flag)
{
	/* Read a message from the queue */
	//printf("A : Reading a message ...\n");
	if(msgrcv(msgqueue_id, qbuf, sizeof(Message) - 4, qbuf->mtype, flag) != -1)
	{
		printf("A : Type: %ld Commond %d Text: %s\n", qbuf->mtype, qbuf->mcommond, qbuf->mtext);
		return 1;
	}
	else
		return 0;
}
